package stalker.room;

import message.Message;
import message.controller.MessageController;
import message.visitor.DefaultRoomMessageVisitor;
import stalker.Stalker;
import stalker.room.roomusers.PublicRoomUsers;
import data.datas.NoHistoryDatas;

public class PublicNoHistoryRoom extends Room {
  
  private final MessageController<Void> room_controller_;
  
  private void checkRep() {
    assert (datas_ != null && 
            room_users_ != null && room_controller_ != null) :
      "Room: fields must not be null";
    assert (Stalker.getStalker(this.getID()) == this) :
      "Room: Inconsistent data";
    assert (Room.getRoomFromName(this.getName()).equals(this)) :
      "Room: Inconsistend data";
  }
  
  /**
   * Creates a new empty room
   * 
   * @requires name is not null, name is unique
   * @param name Name of this room.
   */
  
  private PublicNoHistoryRoom(String name) {
    super(name, new NoHistoryDatas(), new PublicRoomUsers()); 
    room_controller_ = new MessageController<Void>(new DefaultRoomMessageVisitor(this));
    new Thread(room_controller_).start();
    checkRep();
  }
  
  /**
   * Factory interface for creating a new room
   * @requires name is unique, not null
   * @param name Proposed room name
   * @return A new room with the proposed room name
   */
  public static PublicNoHistoryRoom create(String name) {
    assert (name != null) :
      "Room: name not null";
    assert (!Room.isTaken(name)) :
      "Room: name must be unique";
    return new PublicNoHistoryRoom(name);
  }
  
  /**
   * Factory interface for creating a default named room
   * @return A new room with default name (i.e, room + room ID)
   */
  public static PublicNoHistoryRoom create() {
    long proposed_number = Stalker.getStalkerCount();
    while (Room.isTaken("room " + proposed_number)) {
      ++proposed_number;
    }
    return new PublicNoHistoryRoom("room " + proposed_number);
  }

  /**
   * Accepts a request
   * @requires request not null
   * @param request A request made to the room
   */
  @Override
  public void acceptMessage(Message message) {
    assert message != null;
    room_controller_.enqueueMessage(message);
  }
  
}
